package com.lxr.test5002;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.cn.techvision.systemtoolservice.SystemTool;
import com.lxr.test5002.base.BaseActivity;
import com.lxr.test5002.base.Constants;
import com.sdk.devicemanager.DEVICEcon;

import butterknife.BindView;


/**
 * 类描述:[USB测试]
 * 说明:
 *
 * @author: LXR-zsm
 * @Date: 2023/12/18 11:05
 */
public class USBActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.tvUsbStatus)
    TextView tvUsbStatus;
    @BindView(R.id.tbUsb)
    ToggleButton tbUsb;
    @BindView(R.id.tvUsbTip)
    TextView tvUsbTip;
    @BindView(R.id.tv_battery)
    TextView mTvBatteryLevel;
    private DEVICEcon devicEcon;
    private SystemTool systemTool;
    private static final int STATE_IN = 1;
    private static final int STATE_OUT = 2;
    private int mCurrentState;

    public USBActivity() {
        super(R.layout.activity_usb);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        devicEcon = new DEVICEcon(this);
        systemTool = new SystemTool();
        tbUsb.setOnCheckedChangeListener(this);
        tbUsb.setChecked(true);
        registerBroadcast();
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }


    @Override
    public void onCheckedChanged(CompoundButton cb, final boolean isChecked) {
        switch (cb.getId()) {
            case R.id.tbUsb:
                openCloseUsb(isChecked);
                break;
            default:
                break;
        }
    }

    /**
     * 打开关闭USB口
     */
    private void openCloseUsb(boolean isChecked) {
        if (isChecked) {
            boolean open = systemTool.startAdbServer();
            tvUsbStatus.setText(String.format("ADB打开%s", open ? "成功" : "失败"));
        } else {
            boolean open = systemTool.stopAdbServer();
            tvUsbStatus.setText(String.format("ADB关闭%s", open ? "成功" : "失败"));
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRecevier != null) {
            USBActivity.this.unregisterReceiver(mRecevier);
        }
    }

    private USBPlugBroadcast mRecevier;

    private void registerBroadcast() {
        mRecevier = new USBPlugBroadcast();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.hardware.usb.action.USB_STATE");
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        USBActivity.this.registerReceiver(mRecevier, filter);
    }


    class USBPlugBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.e(Constants.LOG_TAG, "action-->" + action);
            if ("android.hardware.usb.action.USB_STATE".equals(action)) {
                Log.e(Constants.LOG_TAG, "USBPlugBroadcast-->" + action);
                boolean connected = intent.getExtras().getBoolean("connected");
                if (connected) {
                    Log.e(Constants.LOG_TAG, "USBPlugBroadcast-->usbIn");
                    if (mCurrentState == STATE_IN) {
                        return;
                    }
                    mCurrentState = STATE_IN;
                    tvUsbTip.setText("USB已连接 \n请查看状态栏USB是否连接");
                } else {
                    Log.e(Constants.LOG_TAG, "USBPlugBroadcast-->usbOut");
                    if (mCurrentState == STATE_OUT) {
                        return;
                    }
                    mCurrentState = STATE_OUT;
                    tvUsbTip.setText("USB未连接 !");
                }
            } else {
                assert action != null;
                if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                    int level = intent.getIntExtra("level", 0);
                    mTvBatteryLevel.setText(String.format("当前电量：%d%%", level));
                }
            }
        }
    }
}